'use strict';

const Service = require('egg').Service;

const fs=require('fs');
const pump = require('mz-modules/pump');

class UploadService extends Service {
    async uploadImages() {
        let parts = this.ctx.multipart({ autoFields: true });
        let files = {};               
        let stream;
        while ((stream = await parts()) != null) {
            if (!stream.filename) {          
              break;
            }       
            let fieldname = stream.fieldname;  //file表单的名字
    
            //上传图片的目录
            let dir=await this.service.tools.getUploadFile(stream.filename);

            let target = dir.uploadDir;
            let writeStream = fs.createWriteStream(target);
    
            await pump(stream, writeStream);  
    
            files=Object.assign(files,{
              [fieldname]:dir.saveDir    
            })
    
           //生成缩略图
           let jimpImgDir=await this.service.tools.jimpImg(target);
    
           return {
            saveImgDir:dir.saveDir,
            jimpImgDir:jimpImgDir
           }
        }     
    }
  
    async uploadFiles() {
  
    }
}

module.exports = UploadService;
